package com.eryos.android.cigarettecounter.tool;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.util.Log;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class GoogleAnalyticsProvider {


	private static final String GOOGLE_ANALYTICS_TRACKER_ID = "UA-8541799-7";
	
	public static final int SCOPE_VISITOR_LEVEL     = 1;           
	public static final int SCOPE_SESSION_LEVEL     = 2;           
	public static final int SCOPE_PAGE_LEVEL        = 3;  

	private static final String APP_VERSION_TAG 	= "";
	private static final String ANDROID_VERSION_TAG = "";
	private static final String SDK_TAG 			= "";
	private static final String BRAND_TAG 			= "";
	private static final String DEVICE_TAG 			= "";
	
	
	private static boolean trackerStarted = false;
	private static GoogleAnalyticsTracker tracker;
	private static Context context;

	private GoogleAnalyticsProvider(){
		
	}
	
	public static void startTracker(Context _context){
		if ( tracker == null ) {
			context = _context;
			tracker = GoogleAnalyticsTracker.getInstance();
			Log.d("GoogleAnalytics", "Tracked initialized ...");
		}
		if ( ! isTrackerStarted() ){
			tracker.start(GOOGLE_ANALYTICS_TRACKER_ID, context);
			trackerStarted = true;
			Log.d("GoogleAnalytics", "Tracked started ...");
		}
	}

	public static void stopTracker(){
		if ( isTrackerValid() ) {
			tracker.dispatch();
			tracker.stop();
			trackerStarted = false;
			Log.d("GoogleAnalytics", "Tracker stoped");
		}
	}

	
	public static void trackPage(String page){
		if ( isTrackerValid() ){
			tracker.trackPageView(page);
		}
	}
	
	public static void dispatch(){
		Log.d("GoogleAnalytics", "dispatch");
		tracker.dispatch();
	}
	
	public static void trackCustomVars(){
		if ( isTrackerValid() ){
			
	    	try {
				PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);                           
				String appVersion = packageInfo.versionName;   
				
				String androidVersion = Build.VERSION.RELEASE;
				
				String sdk = Build.VERSION.SDK;
				
				String brand = Build.MANUFACTURER; 
				
				String device = Build.MANUFACTURER+" : "+Build.MODEL; 
				
				setCustomVar(SCOPE_VISITOR_LEVEL, APP_VERSION_TAG, appVersion);
				setCustomVar(SCOPE_VISITOR_LEVEL, ANDROID_VERSION_TAG, androidVersion);
				setCustomVar(SCOPE_VISITOR_LEVEL, SDK_TAG, sdk);
				setCustomVar(SCOPE_VISITOR_LEVEL, BRAND_TAG, brand);
				setCustomVar(SCOPE_VISITOR_LEVEL, DEVICE_TAG, device);

	    	} catch (NameNotFoundException e) {
				Log.e("GoogleAnalytics", "initTracker", e);
			} 
		}
	}

	public static boolean setCustomVar(int scope, String tag, String value){
		if ( isTrackerValid() ){
			boolean res = tracker.setCustomVar(1, tag, value, scope);
			Log.d("GoogleAnalytics", "Track : "+tag+" : "+res);
			return res;
		}else
			return false;
	}
	
	public static boolean isTrackerStarted() {
		return trackerStarted;
	}
	
	public static boolean isTrackerValid(){
		if ( tracker!= null && isTrackerStarted() )
			return true;
		else 
			return false;
	}
}
